package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import com.scau.service.IEmployeeService;
import com.scau.vo.Employee;

/**
 * @author Jinglun
 * @create 2017年4月20日
 */
public class EmployeeAction extends SuperAction {
	private String result;// 属性result，用于异步传输数据交互
	
	private Employee employee;
	private IEmployeeService employeeService;
	
	public String saveEmployee(){
		Employee empl = getEmployee();
		
		if(employeeService.saveEmployee(empl)){
			List<Employee> listEmpl = employeeService.listEmployee();
			JSONArray jsonArray = JSONArray.fromObject(listEmpl.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
	}
	
	public String listEmployee(){
		List<Employee> listEmpl = employeeService.listEmployee();
		JSONArray jsonArray = JSONArray.fromObject(listEmpl.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	public String deleteEmployee(){
		Employee employee = getEmployee();
		
		if(employeeService.deleteEmployee(employee.getId())){
			List<Employee> listEmpl = employeeService.listEmployee();
			JSONArray jsonArray = JSONArray.fromObject(listEmpl.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
	}
	
	public String updateEmployee(){
		Employee employee = getEmployee();
		
		if(employeeService.updateEmployee(employee)){
			List<Employee> listEmpl = employeeService.listEmployee();
			JSONArray jsonArray = JSONArray.fromObject(listEmpl.toString());
			result = jsonArray.toString();
			return "success";
		} else return "error";
	}
	
	// get,set methods
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public Employee getEmployee() {
		return employee;
	}
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	
	
}
